<cfcomponent displayname="CrafterDocumentCollection" extends="crafter.CrafterCollection" output="no" accessors="yes">
	
	
	<cfproperty name="crafterFactory" type="crafter.CrafterFactory" />

	
	<!--- init() --->
	<cffunction name="init" access="public" returntype="any" output="no">
		<cfargument name="crafterFactory" type="crafter.crafterFactory" required="yes" />
		<cfargument name="collectionName" type="string" required="no" default="" />
		<cfscript>
			super.init(
				collectionName = arguments.collectionName	   
			);
			setCrafterFactory(arguments.crafterFactory);
			return this;
		</cfscript>
	</cffunction>
	
	
	<!--- getItem() --->
	<cffunction name="getItem" access="public" returntype="any" output="no">
		<cfargument name="index" type="numeric" required="yes" />
		<cfscript>
		
			// Grab the item
			// We're not denoting this with an "o" prefix here because it's possible
			// it's not going to be an object, so it could be misleading later on.
			local.item = super.getItem(arguments.index);
			
			
			/*
				Look to see what type of object it is, if the item is a CrafterDocument,
				return it immediately.
				
				If "org.rivetlogic.crafter.core.accessor.impl.NavTreeImpl", then we need
				to create a new document object and replace it with that.
			*/
			if (IsInstanceOf(local.item,"org.rivetlogic.crafter.core.accessor.impl.NavTreeImpl")) {
				
				
				try {
					
					local.oDoc = getCrafterFactory().getDocument(
						path = local.item.getNavItem().getItemUrl()
					);
					
					setItem(
						index = arguments.index,
						item = local.oDoc
					);
					
					return(local.oDoc);
					
				}
				catch(Any e) {
					if (getCrafterFactory().getDebugOutput()) {
						writedump(local.item.getNavItem().getItemUrl());
						writedump(e);
						abort;
					}
					else {
						rethrow();	
					}
				}		
			}
			
			else if (IsInstanceOf(local.item,"crafter.CrafterDocument")) {
				return local.item;	
			}
			
			else if (IsInstanceOf(local.item, "crafter.CrafterListCollection")) {
				return local.item;			  
			}
			
			else {
				
				local.oDoc = getCrafterFactory().getDocument(
					path = local.item
				);
				
				setItem(
					index = arguments.index,
					item = local.oDoc
				);
				
				return(local.oDoc);
			}
			
		</cfscript>
		
		
	</cffunction>
	
	
	<!--- requireSetting()  --->
	<cffunction name="requireSetting" access="public" returntype="any" hint="" output="no">
		<cfargument name="xpath" type="string" required="yes" />
		<cfset var local = {} />
		<cfset local.oWorkingSet = getCollection() />
		<cfset local.arNewCollectionArray = ArrayNew(1) />
		
		
		<!--- Loop over document collection --->
		<cfloop from="1" to="#getCollectionLength()#" index="i">
			<!--- Search document for xPath --->			
			<cfset local.arResults = XmlSearch(getItem(i).getXML(),Arguments.xPath) />
			<cfif (NOT ArrayIsEmpty(local.arResults))>
				<cfset ArrayAppend(local.arNewCollectionArray,getItem(i)) />
			</cfif>
		</cfloop>
		
		<cfset replaceCollection(local.arNewCollectionArray) />
		<cfreturn this />
	</cffunction>
	
	
	<!--- requireTags() --->
	<cffunction name="requireTags" access="public" returntype="any" hint="" output="no">
		<cfargument name="list" type="string" required="yes" />
		<cfargument name="type" type="string" required="no" default="system" />

		
		<cfreturn this />
	</cffunction>
	
	
	<!--- getFileList() --->
	<cffunction name="getFileList" access="public" returntype="string" hint="" output="no">
		<cfscript>
			local.sFileList = "";
			for (local.i = 1; local.i LTE getCollectionLength(); local.i++) {
				local.item = lazyGetItem(local.i);
				if (IsInstanceOf(local.item,"org.rivetlogic.crafter.core.accessor.impl.NavTreeImpl")) {
					local.sFileList = ListAppend(local.sFileList, local.item.getNavItem().getItemURL());
				}
				else if (IsInstanceOf(local.item,"crafter.CrafterDocument")) {
					local.sFileList = ListAppend(local.sFileList, local.item.getFilePath());
				}
				else {
					local.sFileList = ListAppend(local.sFileList, local.item);
				}
			}
			return local.sFileList;
		</cfscript>	
	</cffunction>
	
	
	<!--- removeItemsViaCollection() --->
	<cffunction name="removeItemsViaCollection" access="public" returntype="any" hint="" output="no">
		<cfargument name="collection" type="any" required="yes" />
		<cfscript>
			local.arOriginalCollection = getCollection();
			local.arNewCollection = ArrayNew(1);
			local.sFileList = arguments.collection.getFileList();
			
			for (local.i = 1; local.i LTE getCollectionLength(); local.i++) {
				
				local.item = lazyGetItem(local.i);
				
				if (IsInstanceOf(local.item,"org.rivetlogic.crafter.core.accessor.impl.NavTreeImpl")) {
					local.sSearchValue = local.item.getNavItem().getItemURL();
				}
				else if (IsInstanceOf(local.item,"crafter.CrafterDocument")) {
					local.sSearchValue = local.item.getFilePath();
				}
				else {
					local.sSearchValue = local.item;
				}
				
				local.sSearchValueWithSlash = "/" & local.sSearchValue;
				
				if ((NOT ListFind(local.sFileList, local.sSearchValue)) AND (NOT ListFind(local.sFileList, local.sSearchValueWithSlash))) {
					ArrayAppend(local.arNewCollection, local.item);	
				}
				
			}
			
			replaceCollection(local.arNewCollection);
			return this;
		</cfscript>
	</cffunction>
	
	
</cfcomponent>